﻿#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <assert.h>
#include<vector>
#include<iostream>
using namespace std;
enum Color
{
	Red,
	Black
};
//template<class K, class V>
//struct RBTreeNode
//{
//	pair<K, V> _kv;
//	RBTreeNode<K, V>* _parent;
//	RBTreeNode<K, V>* _left;
//	RBTreeNode<K, V>* _right;
//	Color _col;
//	RBTreeNode(const pair<K, V>& kv)
//		:_kv(kv)
//		, _left(nullptr)
//		, _right(nullptr)
//		, _parent(nullptr)
//	{
//	}
//};
template<class T>
struct RBTreeNode
{
	T _data;//存放数据，可以传递T为pair类型
	RBTreeNode<T>* _parent;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	Color _col;
	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{
	}
};
template<class T,class Ref,class Ptr>
struct _TreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef _TreeIterator<T, Ref, Ptr> Self;//Ref:引用，Ptr:指针
	Node* _node;
	Node* _root;
	_TreeIterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{ }
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}
	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}
	bool operator==(const Self& s)
	{
		return _node == s._node;
	}
	//后置++
	Self operator++(int)
	{
		Self tmp = *this;  // 保存当前状态
		++(*this);         // 调用前置++实现
		return tmp;        // 返回旧值
	}
	//后置--
	Self operator--(int)
	{
		Self tmp = *this;  // 保存当前状态
		--(*this);         // 调用前置--实现
		return tmp;        // 返回旧值
	}
	//前置++
	Self& operator++()
	{
		//中序遍历，这个时候就需要访问右孩子
		if (_node->_right)
		{
			//找右子树的最左结点
			_node = _node->_right;
			while (_node->_left)
			{
				_node = _node->_left;
			}
		}
		else
		{
			//右子树为空
			//对应15结点或者50结点
			Node* cur = _node;
			Node* parent = cur->_parent;
			//当parent不为空，且cur为parent的右孩子是继续往上
			//因为这代表该子树已经遍历完了
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			//要么cur是parent的左孩子，这个时候就可以直接用_node=parent
			//这两种情况都可以这样写
			_node = parent;
		}
		return *this;
	}
	//前置--
	Self& operator--()
	{
		if (_node == nullptr) // end()
		{
			// --end()，特殊处理，⾛到中序最后⼀个结点，整棵树的最右结点
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else if (_node->_left)
		{
			//代表该子树还没遍历完
			//找左子树的最右结点
			_node = _node->_left;
			while (_node->_right)
			{
				_node = _node->_right;
			}
		}
		else
		{
			//该子树被遍历完
			//向上查找，直到查找到根或者该孩子为父亲的右孩子
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
};
template<class K,class T,class KeyOfT>
class RBTree
{
public:
	typedef RBTreeNode<T> Node;
	typedef _TreeIterator<T, T&, T*> Iterator;
	typedef _TreeIterator<T, const T&, const T*> ConstIterator;
	void RotateR(Node* parent)
	{
		assert(parent != nullptr);
		Node* subL = parent->_left;
		assert(subL != nullptr);
		Node* subLR = subL->_right;
		parent->_left = subLR;
		subL->_right = parent;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		Node* parentParent = parent->_parent;
		parent->_parent = subL;
		if (parent == _root)
		{
			subL->_parent = nullptr;
			_root = subL;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
			subL->_parent = parentParent;
		}
	}
	void RotateL(Node* parent)
	{
		assert(parent != nullptr);
		Node* subR = parent->_right;
		assert(subR != nullptr);
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}
		subR->_left = parent;
		Node* parentParent = parent->_parent;
		parent->_parent = subR;
		if (parent == _root)
		{
			subR->_parent = nullptr;
			_root = subR;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}
	pair<Iterator,bool> Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = Black;
			return { Iterator(_root,_root),true };//隐式类型转换
		}
		KeyOfT kot;
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kot(cur->_data) < kot(data))
		    {
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return { Iterator(cur,_root),true };
			}
		}
		cur = new Node(data);
		Node* newnode = cur;
		cur->_parent = parent;
		if (kot(parent->_data) > kot(data))
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_col = Red;
		while (parent && parent->_col == Red)
		{
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == Red)
				{
					uncle->_col = parent->_col = Black;
					grandfather->_col = Red;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_left == cur)
					{
						RotateR(grandfather);
						parent->_col = Black;
						grandfather->_col = Red;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = Black;
						grandfather->_col = Red;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == Red)
				{
					uncle->_col = parent->_col = Black;
					grandfather->_col = Red;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_right == cur)
					{
						parent->_col = Black;
						grandfather->_col = Red;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						grandfather->_col = Red;
						cur->_col = Black;
					}
					break;
				}
			}
		}
		_root->_col = Black;
		return { Iterator(newnode,_root),true };
	}
	Iterator Begin()
	{
		Node* minleft = _root;
		while (minleft && minleft->_left)
		{
			minleft = minleft->_left;
		}
		//调用_TreeIterator
		//相当于_TreeIterator(minleft)
		return Iterator(minleft, _root);
	}
	Iterator End()
	{
		return Iterator(nullptr, _root);
	}
	ConstIterator Begin() const
	{
		Node* minleft = _root;
		while (minleft && minleft->_left)
		{
			minleft = minleft->_left;
		}
		return ConstIterator(minleft, _root);
	}
	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}
	bool Find(const K& key)
	{
		KeyOfT kot;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < key)
			{
				cur = cur->_right;
			}
			else if (kot(cur->_data) > key)
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}
		return false;
	}
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		if (root->_col == Red && root->_parent->_col == Red)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == Black)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}
	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == Red)
			return false;
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == Black)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		_InOrder(root->_right);
	}
private:
	Node* _root = nullptr;
};